706D - Vasiliy's Multiset - CodeForces Solution


binary search bitmasks data structures trees *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(x...) 42
#endif
const int N = 2e5 + 5;
int a[N * 31], timer = 0, bor[N * 31][2], mx = 31;


void add(int x) {
    int root = 0;
    a[root]++;
    for(int i = mx - 1; i >= 0; i--) {
        int bit = ((x >> i) & 1);
        if(bor[root][bit] == 0) bor[root][bit] = ++timer;
        root = bor[root][bit];
        a[root]++;
    }
}

void del(int x) {
    int root = 0;
    a[root]--;
    for(int i = mx - 1; i >= 0; i--) {
        int bit = ((x >> i) & 1);
        root = bor[root][bit];
        a[root]--;
    }
}

int go(int x) {
    int root = 0, res = 0;
    for(int i = mx - 1; i >= 0; i--) {
        int bit = ((x >> i) & 1);
        int need = (bit ^ 1);
        if(bor[root][need] && a[bor[root][need]]) {
            root = bor[root][need];
            res |= (1LL << i);
        } else root = bor[root][bit];
    }
    return res;
}

void solve() {
    int n, x;
    cin >> n;
    char c;
    add(0);
    for(int i = 1; i <= n; i++) {
        cin >> c >> x;
        if(c == '+') {
            add(x);
        } else if (c == '-') {
            del(x);
        } else {
            cout << go(x) << "\n";
        }
    }
}


int main() {
    ios_base::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int t = 1;
    while(t--) solve();
    return 0;
}
 	 		   	 	 	  			 	  	    				


Comments

Submit
0 Comments
More Questions

1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array